package com.business_clean.ui.activity.format;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.uitl.ToastUtil;
import com.business_clean.data.mode.format.FormatChildItemList;
import com.business_clean.data.mode.format.FormatEntity;
import com.business_clean.data.mode.format.FormatItemList;
import com.business_clean.databinding.ActivityFormatBinding;
import com.business_clean.ui.adapter.format.FormatAdapter;
import com.business_clean.ui.adapter.format.FormatRowAdapter;
import com.business_clean.viewmodel.request.FormatViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

public class FormatActivity extends BaseActivity<FormatViewModel, ActivityFormatBinding> {
    private FormatAdapter mAdapter;
    private FormatRowAdapter rowAdapter;

    private String project_cat_id;


    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        if (getIntent() != null && getIntent().getExtras() != null) {
            project_cat_id = getIntent().getExtras().getString("project_cat_id");
        }

        mAdapter = new FormatAdapter();
        mViewModel.getFormatAll();
        getMDatabind().recycler.setLayoutManager(new LinearLayoutManager(this));
        getMDatabind().recycler.setAdapter(mAdapter);

        rowAdapter = new FormatRowAdapter();
        getMDatabind().recyclerRow.setLayoutManager(new LinearLayoutManager(this));
        getMDatabind().recyclerRow.setAdapter(rowAdapter);
    }

    @Override
    public void onBindViewClick() {
        rowAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                mAdapter.setList(rowAdapter.getData().get(position).getChild_list());
                rowAdapter.updateItem(rowAdapter.getData().get(position).getId());
            }
        });
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                App.appViewModelInstance.getSelectedFormat().setValue(mAdapter.getData().get(position));
                finish();
            }
        });
    }

    @Override
    public void onRequestSuccess() {
        mViewModel.getFormatEntity().observe(this, new Observer<FormatEntity>() {
            @Override
            public void onChanged(FormatEntity formatEntity) {
                rowAdapter.setList(formatEntity.getList());
                if (!TextUtils.isEmpty(project_cat_id)) {//如果有值 都要去反选
                    for (FormatItemList list : formatEntity.getList()) {
                        for (FormatChildItemList child : list.getChild_list()) {
                            if (project_cat_id.equals(child.getId())) {
                                mAdapter.setList(list.getChild_list());
                                rowAdapter.updateItem(list.getId());
                                mAdapter.updateItem(project_cat_id);
                                break;
                            }
                        }
                    }
                } else {
                    mAdapter.setList(formatEntity.getList().get(0).getChild_list());
                    rowAdapter.updateItem(formatEntity.getList().get(0).getId());
                }

            }
        });
    }
}